package com.dhecp.project.finance.controller;

import com.dhecp.common.annotation.Log;
import com.dhecp.common.annotation.RepeatSubmit;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.core.page.TableDataInfo;
import com.dhecp.common.enums.BusinessType;
import com.dhecp.common.utils.DateUtils;
import com.dhecp.common.utils.TypeUtils;
import com.dhecp.core.constant.ModuleTable;
import com.dhecp.core.layui.GridColumnsPropertyModel;
import com.dhecp.core.utils.ExportExcelUtils;
import com.dhecp.framework.util.ShiroUtils;
import com.dhecp.project.base.domain.OperatorBankInfo;
import com.dhecp.project.common.controller.BaseModuleController;
import com.dhecp.project.common.domain.ModuleNidAndBid;
import com.dhecp.project.common.service.ModuleNidAndBidService;
import com.dhecp.project.finance.domain.TcwCapitalBusinessCost;
import com.dhecp.project.finance.domain.TcwCapitalBusinessCostSon;
import com.dhecp.project.finance.mapper.TcwCapitalBusinessCostSonMapper;
import com.dhecp.project.finance.service.TcwCapitalBusinessCostService;
import com.dhecp.project.hr.domain.OperateCompanyApply;
import com.dhecp.project.system.domain.SysDictionaryForExport;
import com.dhecp.project.system.service.SysDictionaryForFieldService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

/**
 * 运营费用认领单(TcwCapitalBusinessCost)控制层
 *
 */
@Controller
@RequestMapping("/finance/tcwCapitalBusinessCost")
public class TcwCapitalBusinessCostController extends BaseModuleController{

    private String prefix = "finance/tcwCapitalBusinessCost";
    private String moduleTable = ModuleTable.TCW_CAPITALBUSINESSCOST;
    private String moduleTable1 = ModuleTable.TCW_CAPITALBUSINESSCOSTSON;

    private String moduleTable2 = ModuleTable.TB_OPERATORBANKINFO;

    @Autowired
    private SysDictionaryForFieldService dictionaryForFieldService;

    @Autowired
    private TcwCapitalBusinessCostService tcwCapitalBusinessCostService;

    @Autowired
    private TcwCapitalBusinessCostSonMapper tcwCapitalBusinessCostSonMapper;

    @Autowired
    private GridColumnsPropertyModel gridColumnsPropertyModel;

    @Autowired
    private ModuleNidAndBidService moduleNidAndBidService;


    /**
     * @Description: 打开查询列表页
     * @Param: [params]
     * @return: org.springframework.web.servlet.ModelAndView
     * @Author: hlf
     * @date: 2022-08-15 15:42
     */
    @RequiresPermissions("finance:tcwCapitalBusinessCost:view")
    @GetMapping()
    public ModelAndView view(@RequestParam Map<String, Object> params) {
        params.put("moduleTable", moduleTable);
        params.put("entity", new TcwCapitalBusinessCost()); //生成表格gridColumns对应的实体，用于获取实体属性生成表格字段
        params.put("prefix", prefix + "/tcwCapitalBusinessCost"); //页面路径
        params.put("toolBarWidth", 0);
        params.put("checkBox", "yes");

        ModelAndView mv = this.openListPage(params);
        mv.addObject("finance", "tcwCapitalBusinessCost");
        return mv;
    }

    /**
     * @Description: 查询页 列表数据
     * @Param: [params]
     * @return: com.dhecp.common.core.page.TableDataInfo
     * @Author: hlf
     * @date: 2022-08-15 15:43
     */
    @RequiresPermissions("finance:tcwCapitalBusinessCost:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam Map<String, Object> params) {
        params.put("moduleTable", moduleTable);
        List<TcwCapitalBusinessCost> list = new ArrayList<>();
        try {
            list = tcwCapitalBusinessCostService.queryList(params);
        } catch (Exception e) {
            e.printStackTrace();
            return getDataTable(this.getExceptionMessage(e).toString());
        }
        return getDataTable(list);
    }

    /**
     * @Description: 数据导出Excel
     * @Param: [params]
     * @return: com.dhecp.common.core.domain.AjaxResult
     * @Author: hlf
     * @date: 2022-08-15 15:43
     */
    @RequiresPermissions("finance:tcwCapitalBusinessCost:export")
    @Log(title = "运营费用认领单导出", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult export(@RequestParam Map<String, Object> params) throws Exception {
        params.put("moduleTable", moduleTable);
        params.put("entity", new TcwCapitalBusinessCost());

        List<TcwCapitalBusinessCost> list = tcwCapitalBusinessCostService.queryList(params);

        params.put("getFieldAttributesType", "export");
        List<SysDictionaryForExport> fieldList = dictionaryForFieldService.queryExportFieldList(params);
        ExportExcelUtils<TcwCapitalBusinessCost> util = new ExportExcelUtils<>(moduleTable, new TcwCapitalBusinessCost());
        return util.exportExcel(list, fieldList, "运营费用认领单", "export");
    }


    /**
     * @Description: 打开制单页
     * @Param: nid 必要参数：单据编码（新增时传默认值 0 ，修改、查看时传单据实际值）
     * @return: org.springframework.web.servlet.ModelAndView
     * @Author: hlf
     * @date: 2022-08-15 15:42
     */
    @RequiresPermissions("finance:tcwCapitalBusinessCost:view")
    @GetMapping("/edit")
    public ModelAndView editPage(@RequestParam Map<String, Object> params) {
        if (params.get("nid") == null || params.get("nid") == "") {
            params.put("nid", "0");
        }
        params.put("moduleTable", moduleTable);
        params.put("entity", new TcwCapitalBusinessCost()); //用于获取实体属性名取代对应字典字段名，解决大小写敏感问题
        params.put("moduleUrl", prefix); //页面上级路径
        params.put("prefix", prefix + "/tcwCapitalBusinessCost_edit"); //页面路径
        Subject userSubject = ShiroUtils.getSubject();
        int enableEdit = 0;
        boolean vType = false;
        if (params.get(enableEdit) == null || "1".equals(params.get(enableEdit).toString())) {
            if (userSubject.isPermitted("finance:tcwCapitalBusinessCost:edit")) { //当前登录用户主体是否有修改的权限
                enableEdit = 1;
            }
        }

        try {
            Map<String, Object> headData = new HashMap<>();        //表头的业务数据
            TcwCapitalBusinessCost businessData = tcwCapitalBusinessCostService.queryFirstRecord(params);
            if (businessData == null) { //未查询到对应单据，页面初始化值
                //List<OperateCompanyApply> companyInfo = tcwCapitalBusinessCostService.queryOperateCompanyApplyByVGsBm("GS0874");
                ModuleNidAndBid moduleNidAndBid = moduleNidAndBidService.queryModuleNidAndBidInitial(params);
                TcwCapitalBusinessCost tcwCapitalBusinessCost = new TcwCapitalBusinessCost();
                tcwCapitalBusinessCost.setNid("0");
                tcwCapitalBusinessCost.setBid(moduleNidAndBid.getBid());
                tcwCapitalBusinessCost.setCode(moduleNidAndBid.getBid());
                tcwCapitalBusinessCost.setCreateTime(new Date());
                tcwCapitalBusinessCost.setCid(ShiroUtils.getMandatorCid());
                tcwCapitalBusinessCost.setCidName(ShiroUtils.getMandatorCidName());
                tcwCapitalBusinessCost.setbUser(ShiroUtils.getMandatorId());
                tcwCapitalBusinessCost.setbUserName(ShiroUtils.getMandatorName());
                tcwCapitalBusinessCost.setbDept(ShiroUtils.getMandatorDeptPath());
                tcwCapitalBusinessCost.setbDeptName(ShiroUtils.getMandatorDeptName());
                tcwCapitalBusinessCost.setbPosition(ShiroUtils.getMandatorPositionNameDa());
                tcwCapitalBusinessCost.setaUser(ShiroUtils.getUserId());
                tcwCapitalBusinessCost.setaUserName(ShiroUtils.getUserName());
                tcwCapitalBusinessCost.setaDept(ShiroUtils.getUserDeptPath());
                tcwCapitalBusinessCost.setaDeptName(ShiroUtils.getUserDeptName());
                tcwCapitalBusinessCost.setaPosition(ShiroUtils.getUserPositionNameDa());
                //if(companyInfo!=null && companyInfo.size()>0){
                    //tcwCapitalBusinessCost.setAccountingCompanyAbbreviation(companyInfo.get(0).getvGsJc());
                    //tcwCapitalBusinessCost.setAccountingCompanyName(companyInfo.get(0).getvGsMc());
                //}
                headData = TypeUtils.entityToMap(tcwCapitalBusinessCost);
            } else {
                headData = TypeUtils.entityToMap(businessData);
                vType = businessData.getEffectStock();
            }
            params.put("headData", headData); //表头的业务数据

            params.put("checkBox", "yes"); //生成表格gridColumns时包含复选
            ModelAndView mv = this.openEditPage(params);
            mv.addObject("nowTimeStr", DateUtils.getDate());    //当前日期
            mv.addObject("enableEdit", vType ? 0 : enableEdit); //当前页面是否可编辑（0：不可编辑，1：可编辑）
            if (businessData != null) {

                mv.addObject("List", tcwCapitalBusinessCostSonMapper.queryListCode(businessData.getBid()));//详情数据
                mv.addObject("bid", businessData.getBid());//单号
            }
            //取商品子件列表网格
            params.put("moduleTable", moduleTable1);
            params.put("entity", new TcwCapitalBusinessCostSon()); //生成表格gridColumns对应的实体，用于获取实体属性生成表格字段
            params.put("readOnly", vType);//生成列是否可编辑
            params.put("toolBarWidth", 0);//操作区
            params.put("getFieldAttributesType", "pageingrid");//pageingrid：网格字段
            params.put("checkBox", "yes");
            mv.addObject("gridColumnsZJ", gridColumnsPropertyModel.queryGridColumnsPropertyModel(params));

            return mv;
        } catch (Exception e) {
            return this.gotoModelAndViewErrorPage(e.getMessage());    //详细信息
        }

    }

    /**
     * @Description: 保存数据
     * @Param: infoMap 必要参数：非网格字段（包括头部、底部所有显示和隐藏的字段）
     * @Param: infoItems 必要参数：对应实体类的json格式字符串
     * @return: com.dhecp.common.core.domain.AjaxResult
     * @Author: hlf
     * @date: 2022-08-15 15:44
     */
    @RequiresPermissions(value = {"finance:tcwCapitalBusinessCost:add", "finance:tcwCapitalBusinessCost:edit", "finance:tcwCapitalBusinessCost:delete"}, logical = Logical.OR)
    @Log(title = "资金登记单", businessType = BusinessType.UPDATE)
    @PostMapping("/editpost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult editpost(@RequestParam Map<String, Object> params) {
        params.put("moduleTable", moduleTable);
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult = tcwCapitalBusinessCostService.saveData(params);
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }

    /**
     *@Description: 查询前一单NID、BID
     *@Param: [params] nid 必要参数：当前 单据编码
     *@return: com.dhecp.common.core.domain.AjaxResult (data.nid 前一单NID， data.bid 前一单BID)
     *@Author: hlf
     *@date: 2022-05-09 11:40
     */
    @RequiresPermissions("finance:tcwCapitalBusinessCost:view")
    @GetMapping("/lastnid")
    @ResponseBody
    public AjaxResult lastNid(@RequestParam Map<String, Object> params){
        params.put("moduleTable", moduleTable);
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult = tcwCapitalBusinessCostService.queryLastNid(params);
        }catch (Exception e) {
            e.printStackTrace();
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }

    /**
     *@Description: 查询后一单NID、BID
     *@Param: [params] nid 必要参数：当前 单据编码
     *@return: com.dhecp.common.core.domain.AjaxResult (data.nid 后一单NID， data.bid 后一单BID)
     *@Author: hlf
     *@date: 2022-05-09 11:40
     */
    @RequiresPermissions("finance:tcwCapitalBusinessCost:view")
    @GetMapping("/nextnid")
    @ResponseBody
    public AjaxResult nextNid(@RequestParam Map<String, Object> params){
        params.put("moduleTable", moduleTable);
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult = tcwCapitalBusinessCostService.queryNextNid(params);
        }catch (Exception e) {
            e.printStackTrace();
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }

    /**
     * 打开流转页
     * @return
     * @author DH-Xxj
     */
    @RequiresPermissions("finance:tcwCapitalBusinessCost:view")
    @PostMapping("/flowpage")
    @ResponseBody
    public AjaxResult flowPage(@RequestParam Map<String, Object> params){
        params.put("moduleTable", moduleTable);
        params.put("prefix", prefix);
        return openFlowPage(params);
    }

    /**
     *@Description: 流转
     *@Param: [params] infoMap 必要参数：非网格字段（包括头部、底部所有显示和隐藏的字段）
     *@Param: [params] infoItems 必要参数：对应实体类的json格式字符串（仅需 guid）
     *@return: com.dhecp.common.core.domain.AjaxResult
     *@Author: hlf
     *@date: 2022-05-09 11:42
     */
    @RequiresPermissions("finance:tcwCapitalBusinessCost:view")
    @Log(title = "资金登记单", businessType = BusinessType.FLOWGO)
    @PostMapping("/flowgopost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult flowGoSave(@RequestParam Map<String, Object> params){
        params.put("moduleTable", moduleTable);
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= tcwCapitalBusinessCostService.updateFlowGo(params);
        }catch (Exception e) {
            e.printStackTrace();
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }

    /**
     *@Description: 退回
     *@Param: [params] infoMap 必要参数：非网格字段（包括头部、底部所有显示和隐藏的字段）
     *@Param: [params] 必要参数：对应实体类的json格式字符串（仅需 guid）
     *@return: com.dhecp.common.core.domain.AjaxResult
     *@Author: hlf
     *@date: 2022-05-09 11:42
     */
    @RequiresPermissions("finance:tcwCapitalBusinessCost:view")
    @Log(title = "运营费用认领单", businessType = BusinessType.FLOWBACK)
    @PostMapping("/flowbackpost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult flowBackSave(@RequestParam Map<String, Object> params){
        params.put("moduleTable", moduleTable);
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= tcwCapitalBusinessCostService.updateFlowBack(params);
        }catch (Exception e) {
            e.printStackTrace();
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }



    /**
     * @Description: 删除未审核数据
     * @Param: [params]
     * @return: com.dhecp.common.core.domain.AjaxResult
     * @Author: hlf
     * @date: 2022-08-17 14:55
     */
    @RequiresPermissions("finance:tcwCapitalBusinessCost:delete")
    @Log(title = "删除运营费用认领单", businessType = BusinessType.UPDATE)
    @PostMapping("/delete")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult delete(@RequestParam Map<String, Object> params) {
        params.put("moduleTable", moduleTable);
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult = tcwCapitalBusinessCostService.delete(params);
        } catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }

}

